package com.sx.first.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.sx.first.biz.DepartmentBiz;
import com.sx.first.biz.EmployeeBiz;
import com.sx.first.biz.PositionBiz;
import com.sx.first.model.Department;
import com.sx.first.model.Employee;
import com.sx.first.model.Position;

@Controller
@Scope("prototype")
@Namespace("/employee")
@Results({ @Result(name = "list", location = "list.jsp"), @Result(name = "add", location = "add.jsp"),
		@Result(name = "update", location = "update.jsp"), })
public class EmployeeAction extends BaseAction {
	private Employee emp;
	private List<Employee> empList;
	private List<Department> depList;
	private List<Position> posList;
	private EmployeeBiz employeeBiz;
	private DepartmentBiz departmentBiz;
	private PositionBiz positionBiz;
	private File photoFile;
	private String savePath = "E:/A1000phone/image/";
	private String photoFileContentType;
	private String photoFileFileName;
	private String depId;

	@Action(value = "list")
	public String list() {
		empList = employeeBiz.findAll();
		return "list";
	}

	@Action(value = "add")
	public String add() {
		depList = departmentBiz.findAll();
		posList = positionBiz.findAll();
		return "add";
	}

	@Action(value = "doAdd")
	public String doAdd() {
		String url = UUID.randomUUID().toString();
		emp.setPhotoName(photoFileFileName);
		emp.setPhotoPath(url);
		employeeBiz.save(emp);
		File f = new File(savePath + url);
		try {
			FileUtils.copyFile(photoFile, f);
		} catch (IOException e) {
			e.printStackTrace();
		}
		writeJson();
		return null;
	}

	@Action(value = "findPositionByDepId")
	public String findPositionByDepId() {
		HttpServletResponse response = ServletActionContext.getResponse();
		posList = positionBiz.findAllByDepId(Integer.parseInt(depId));
		for (int i = 0; i < posList.size(); i++) {
			posList.get(i).setDep(null);
		}
		String jsonStr = new Gson().toJson(posList);
		writeJsonString(response, jsonStr);
		return null;
	}

	@Action(value = "delete")
	public String delete() {
		employeeBiz.delete(emp.getEmpId());
		writeJson();
		return null;
	}

	@Action(value = "update")
	public String update() {
		emp = employeeBiz.findById(emp.getEmpId());
		depList = departmentBiz.findAll();
		posList = positionBiz.findAll();
		return "update";
	}

	@Action(value = "doUpdate")
	public String doUpdate() {
		String url = UUID.randomUUID().toString();
		emp.setPhotoName(photoFileFileName);
		emp.setPhotoPath(url);
		employeeBiz.update(emp);
		File f = new File(savePath + url);
		try {
			FileUtils.copyFile(photoFile, f);
		} catch (IOException e) {
			e.printStackTrace();
		}
		writeJson();
		return null;
	}

	/***********************/
	public File getPhotoFile() {
		return photoFile;
	}

	public void setPhotoFile(File photoFile) {
		this.photoFile = photoFile;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getPhotoFileContentType() {
		return photoFileContentType;
	}

	public void setPhotoFileContentType(String photoFileContentType) {
		this.photoFileContentType = photoFileContentType;
	}

	public String getPhotoFileFileName() {
		return photoFileFileName;
	}

	public void setPhotoFileFileName(String photoFileFileName) {
		this.photoFileFileName = photoFileFileName;
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public List<Employee> getEmpList() {
		return empList;
	}

	public void setEmpList(List<Employee> empList) {
		this.empList = empList;
	}

	public List<Department> getDepList() {
		return depList;
	}

	public void setDepList(List<Department> depList) {
		this.depList = depList;
	}

	public List<Position> getPosList() {
		return posList;
	}

	public void setPosList(List<Position> posList) {
		this.posList = posList;
	}

	public void setEmployeeBiz(EmployeeBiz employeeBiz) {
		this.employeeBiz = employeeBiz;
	}

	public void setDepartmentBiz(DepartmentBiz departmentBiz) {
		this.departmentBiz = departmentBiz;
	}

	public void setPositionBiz(PositionBiz positionBiz) {
		this.positionBiz = positionBiz;
	}

	public String getDepId() {
		return depId;
	}

	public void setDepId(String depId) {
		this.depId = depId;
	}
}